﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YunHan.BuShi123.BLL;
using YunHan.BuShi123.Model;
using System.Web.Security;

public partial class admin_UpPassWord : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void btnUpPassWord_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(tbxNewPassword.Text.Trim()))
        {
            FineUI.Alert.Show("密码不能空");
            return;
        }
        if (string.IsNullOrEmpty(tbxNewPassword2.Text.Trim()))
        {
            FineUI.Alert.Show("确认密码不能空");
            return;
        }
        if (tbxNewPassword.Text.Trim().Length < 6)
        {
            FineUI.Alert.Show("密码最少6为字符");
            return;
        }
        if (tbxNewPassword2.Text.Trim().Length < 6)
        {
            FineUI.Alert.Show("确认密码最少6为字符");
            return;
        }
        if (!tbxNewPassword.Text.Trim().Equals(tbxNewPassword2.Text.Trim()))
        {
            FineUI.Alert.Show("两次新密码不一致");
            return;
        }
        string passWord = tbxPassword.Text.Trim();
        string newPassWord = tbxNewPassword.Text.Trim();
        int result = 0;

        Admin_Model A = Admin_BLL.GetModelByKeyFiled(MemberID);
        if (A.PassWord != passWord)
        {
            FineUI.Alert.Show("原密码错误");
            return;
        }

        A.PassWord = newPassWord;
        result = Admin_BLL.Update("password=@password", "id=@id", A);

        switch (result)
        {
            case -1:
                FineUI.Alert.Show("用户不存在");
                break;

            case -2:
                FineUI.Alert.Show("原密码错误");
                break;
        }
        if (result > 0)
        {
            FineUI.Alert.Show("修改成功");
        }
        else
        {
            FineUI.Alert.Show("修改失败");
        }
    }
    #region MD5加密
    /// <summary>
    /// 用md5加密
    /// </summary>
    /// <param name="Sourcein">输入的未加密之前的字符串</param>
    /// <returns>加密后的字符串</returns>
    public static string EncodingToMD5(string Sourcein)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider MD5CSP = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] MD5Source = System.Text.Encoding.UTF8.GetBytes(Sourcein);
        byte[] MD5Out = MD5CSP.ComputeHash(MD5Source);
        return Convert.ToBase64String(MD5Out);
    }
    #endregion

    #region 返回MD5加密数据
    public string md5(string str)
    {
        return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
    }
    #endregion
}